package com.maiji.cloud.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@EnableAutoConfiguration
public class MyRedisConfig {
    public static final Integer REDISDATABASENO = 5;

    @Value("${spring.redis.maiji1.host}")
    private String maiji1Host;

    @Value("${spring.redis.maiji1.port}")
    private int maiji1Port;

    @Value("${spring.redis.maiji1.database}")
    private int maiji1Database;

    @Value("${spring.redis.maiji1.password}")
    private String maiji1Password;

    @Value("${spring.redis.maiji2.host}")
    private String maiji2Host;

    @Value("${spring.redis.maiji2.port}")
    private int maiji2Port;

    @Value("${spring.redis.maiji2.database}")
    private int maiji2Database;

    @Value("${spring.redis.maiji2.password}")
    private String maiji2Password;

    private static final int MAX_IDLE = 20; //最大空闲连接数
    private static final int MAX_TOTAL = 100; //最大连接数
    private static final long MAX_WAIT_MILLIS = 1000; //建立连接最长等待时间

    //配置工厂
    public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle, int maxTotal, long maxWaitMillis, int index) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(port);
        if (!StringUtils.isEmpty(password))
            jedisConnectionFactory.setPassword(password);
        if (index != 0)
            jedisConnectionFactory.setDatabase(index);
        jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    //连接池配置
    public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }

    /**
     * 配置RedisTemplate
     * 设置添加序列化器
     * key 使用string序列化器
     * value 使用Json序列化器
     * 还有一种简答的设置方式，改变defaultSerializer对象的实现
     */
    @Bean(name="maiji1RedisTemplate")
    public RedisTemplate<String, Object> maiji1RedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        template.setConnectionFactory(connectionFactory(maiji1Host, maiji1Port, maiji1Password, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, maiji1Database));
        template.afterPropertiesSet();
        return template;
    }

    @Bean(name="maiji2RedisTemplate")
    public RedisTemplate<String, Object> maiji2RedisTemplate() {
        if (StringUtils.isBlank(maiji2Host)) return null;
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        template.setConnectionFactory(connectionFactory(maiji2Host, maiji2Port, maiji2Password, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, maiji2Database));
        template.afterPropertiesSet();
        return template;
    }

}
